﻿using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Xml.Serialization;
using System.Collections.Generic;
using System.Net;
using System.Text;

namespace Simple_ToDo.Desktop
{
    static class SaveAndSync
    {
        public static void Serialize(TaskControl t)
        {
            Stream stream = File.Open("Tasks.dat", FileMode.Create);
            BinaryFormatter bformatter = new BinaryFormatter();

            bformatter.Serialize(stream, t);
            stream.Close();
        }

        public static TaskControl Deserialize()
        {
            TaskControl t = null;
            Stream stream = File.Open("Tasks.dat", FileMode.Open);
            BinaryFormatter bformatter = new BinaryFormatter();

            t = (TaskControl)bformatter.Deserialize(stream);
            stream.Close();

            return t;
        }

        public static void SerializeToXml(TaskControl t)
        {
            XmlSerializer serializer = new XmlSerializer(typeof(TaskControl));
            TextWriter textWriter = new StreamWriter("Tasks.xml");
            serializer.Serialize(textWriter, t);
            textWriter.Close();
        }

        public static TaskControl DeserializeFromXml()
        {
            TaskControl t = null;

            XmlSerializer deserizlizer = new XmlSerializer(typeof(TaskControl));
            TextReader textReader = new StreamReader("Tasks.xml");
            t = (TaskControl)deserizlizer.Deserialize(textReader);
            textReader.Close();
            
            return t;
        }

        public static void UploadToFTP()
        {
            FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://77.47.179.129/Upload/Tasks.xml");
            request.Method = WebRequestMethods.Ftp.UploadFile;

            request.Credentials = new NetworkCredential("anonymous", "anonymous");

            TextReader textReader = new StreamReader("Tasks.xml");
            byte[] fileContents = Encoding.UTF8.GetBytes(textReader.ReadToEnd());
            textReader.Close();
            request.ContentLength = fileContents.Length;

            Stream requestStream = request.GetRequestStream();
            requestStream.Write(fileContents, 0, fileContents.Length);
            requestStream.Close();

            FtpWebResponse response = (FtpWebResponse)request.GetResponse();
            response.Close();
        }

        public static void DownloadFromFTP()
        {
            FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://77.47.179.129/Upload/Tasks.xml");
            request.Method = WebRequestMethods.Ftp.DownloadFile;

            request.Credentials = new NetworkCredential("anonymous", "anonymous");

            FtpWebResponse response = (FtpWebResponse)request.GetResponse();

            Stream responseStream = response.GetResponseStream();
            StreamReader reader = new StreamReader(responseStream);

            Stream stream = File.Open("Tasks.xml", FileMode.Create);
            StreamWriter sw = new StreamWriter(stream);
            sw.Write(reader.ReadToEnd());
            sw.Close();
            stream.Close();

            reader.Close();
            response.Close();  
        }
    }
}
